package com.yyfax.ph.personal.mymoney.cash;

import android.text.TextUtils;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class RebateDetailPresenter extends Presenter<RebateDetailModel, RebateDetailPresenter.View> {
	private View mView;

	public static class Generator implements PresenterFactory<RebateDetailPresenter> {
		@Override
		public RebateDetailPresenter create() {
			return new RebateDetailPresenter();
		}
	}

	public interface View extends IView {
		void refreshRebateDetail(int cnt, int amountSum, List<RebateDetailModel.RebateInfo> list);

		void loadMoreRebateDetail(int cnt, int amountSum, List<RebateDetailModel.RebateInfo> list);

		void getRebateFailure(String tips);
	}

	@Override
	public void attach(View view) {
		mView = view;
		mModel = new RebateDetailModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public void getRebateList(int pageNo, int pageSize, String startDate) {
		RebateDetailModel.RebateParams detail = new RebateDetailModel.RebateParams();
		detail.pageSize = pageSize;
		detail.pageNo = pageNo;
		detail.startDate = startDate;
		if (pageNo > 1) {
			execute(detail, RebateDetailModel.MSG_LOAD_MORE_REBATE_DETAILS, false);
		} else {
			execute(detail, RebateDetailModel.MSG_REFRESH_REBATE_DETAILS, false);
		}
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getDetailResult(RebateDetailModel.RebateDetailResult result) {
		if (isResponseSuccess(result)) {
			if (result.requestCode == RebateDetailModel.MSG_REFRESH_REBATE_DETAILS) {
				mView.refreshRebateDetail(result.getContent().cnt, result.getContent().amountSum, result.getContent().accountDetailForDrawList);
			} else {
				mView.loadMoreRebateDetail(result.getContent().cnt, result.getContent().amountSum, result.getContent().accountDetailForDrawList);
			}
			return;
		}
		mView.getRebateFailure(TextUtils.isEmpty(result.getMsg()) ? "获取数据失败!" : result.getMsg());
	}
}
